package game.lan;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.StringConstants;
import game.entity.PlayingRole;
import game.session.SessionManager;
import io.netty.channel.Channel;
import lion.lan.ILanIoExecutor;
import lion.lan.LanClientSession;
import lion.lan.Netty4LanClient;
import lion.netty4.message.RequestMessage;
import lion.netty4.message.SendToMessage;

public class RoomLanClientManager implements ILanIoExecutor {

	private Netty4LanClient lanClient;

	private Map<String, LanClientSession> gsConnectionSessionMap = new HashMap<String, LanClientSession>();

	private static Logger logger = LoggerFactory.getLogger(RoomLanClientManager.class);

	public RoomLanClientManager() {
		lanClient = new Netty4LanClient(this,null);
	}

	public void shutdown() {
		if (lanClient != null) {
			lanClient.close();
		}
	}

	public boolean connect(String host, int port) {
		LanClientSession gsConnectionSession = gsConnectionSessionMap.get(host + StringConstants.SEPARATOR_DI + port);
		if (gsConnectionSession == null) {
			gsConnectionSession = new LanClientSession(lanClient);
			gsConnectionSessionMap.put(host + StringConstants.SEPARATOR_DI + port, gsConnectionSession);
		}
		// already connected
		if (gsConnectionSession.isConnected()) {
			return true;
		}
		boolean ret = false;
		synchronized (gsConnectionSession) {
			try {
				gsConnectionSession.connect(host, port);
				ret = true;
			} catch (Exception e) {
				logger.error("lan connect to " + host + ":" + port + " error!", e);
			}
		}
		return ret;
	}

	@Override
	public void execute(Channel channel, RequestMessage msg) throws IOException {
		logger.info("reveive msgCode={}", msg.getMsgCode());
		switch (msg.getMsgCode()) {
		case 20004:
			break;
		case 20002:
			// GsSyncRequest.getInstance().doReceive(msg);
			break;
		default:
			break;
		}
	}

	public void sendCraftCancel2Cross(String crossHost, int crossPort, Integer playerId, Integer serverId) {
		LanClientSession gsConnectionSession = gsConnectionSessionMap
				.get(crossHost + StringConstants.SEPARATOR_DI + crossPort);
		SendToMessage sendToMessage = new SendToMessage(gsConnectionSession.alloc(), 30003);
		sendToMessage.writeInt(playerId);
		sendToMessage.writeInt(serverId);
		gsConnectionSession.writeAndFlush(sendToMessage);
	}

	public void sendCrossBossView2Cross(String crossHost, int crossPort, int playerId, Integer serverId) {
		LanClientSession gsConnectionSession = gsConnectionSessionMap
				.get(crossHost + StringConstants.SEPARATOR_DI + crossPort);
		SendToMessage sendToMessage = new SendToMessage(gsConnectionSession.alloc(), 30007);
		sendToMessage.writeInt(playerId);
		sendToMessage.writeInt(serverId);
		// send
		gsConnectionSession.writeAndFlush(sendToMessage);
	}

	public void sendPay2Ls(String loginHost, long loginPort, long randOrderId, int serverId, String openId,int productId) {
		LanClientSession gsConnectionSession = gsConnectionSessionMap
				.get(loginHost + StringConstants.SEPARATOR_DI + loginPort);
		SendToMessage sendToMessage = new SendToMessage(gsConnectionSession.alloc(), 11001);
		sendToMessage.writeLong(randOrderId);
		sendToMessage.writeInt(serverId);
		sendToMessage.writeString(openId);
		sendToMessage.writeInt(productId);
		// send
		gsConnectionSession.writeAndFlush(sendToMessage);
	}

	public void sendCalculateHallServer(String hostName, int port,int score[],List<Integer> players, int coin_di,String roomdis,short count,int roomType) {
		LanClientSession gsConnectionSession = gsConnectionSessionMap
				.get(hostName + StringConstants.SEPARATOR_DI + port);
		
		SendToMessage sendToMessage = new SendToMessage(gsConnectionSession.alloc(), 21001);
		sendToMessage.writeInt(coin_di);
		sendToMessage.writeInt(score.length);
		for(int soclen=0;soclen<score.length;soclen++) {
			sendToMessage.writeInt(score[soclen]);
		}
		sendToMessage.writeInt(players.size());
		for(Integer aplayer:players)
			sendToMessage.writeInt(aplayer);
		sendToMessage.writeString(roomdis);
		sendToMessage.writeShort(count);
		sendToMessage.writeInt(roomType);
		gsConnectionSession.writeAndFlush(sendToMessage);
	}
	/**
	 * 房卡结算
	 * */
	public void sendCalculateFangKa(String hostName, int port,short fee,byte type,List<Integer> players) {
		LanClientSession gsConnectionSession = gsConnectionSessionMap
				.get(hostName + StringConstants.SEPARATOR_DI + port);
		SendToMessage sendToMessage = new SendToMessage(gsConnectionSession.alloc(), 21003);
		sendToMessage.writeShort(fee);
		sendToMessage.writeByte(type);
		sendToMessage.writeInt(players.size());
		for(Integer aplayer:players) 
			sendToMessage.writeInt(aplayer);
		gsConnectionSession.writeAndFlush(sendToMessage);
	}
    /**
     * 金豆房费扣去
     * jindouDi:该局金豆的底
     * players:玩家列表
     * */
	public void sendCalculateJindouFee(String hostName, int port,int jindouDi,List<Integer> players) {
		LanClientSession gsConnectionSession = gsConnectionSessionMap
				.get(hostName + StringConstants.SEPARATOR_DI + port);
		SendToMessage sendToMessage = new SendToMessage(gsConnectionSession.alloc(), 21005);
		sendToMessage.writeInt(jindouDi);
		sendToMessage.writeInt(players.size());
		for(Integer aplayer:players) 
			sendToMessage.writeInt(aplayer);
		gsConnectionSession.writeAndFlush(sendToMessage);
		
	}
}